using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Lab2Texture
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager m_graphics;
        private SpriteBatch m_spriteBatch;

        private Matrix World;
        private Matrix View;
        private Matrix Projection;

        private Effect m_effect;
        private Model m_modelSphere;
        private double m_fov = 70.0;

        private Texture2D m_texture;
        private Texture2D m_normalMap;
        private Texture2D m_glossMap;
        private Texture2D m_nightMap;

        public Game1()
        {
            m_graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            m_spriteBatch = new SpriteBatch(GraphicsDevice);

            m_effect = Content.Load<Effect>("Effects/Effect1");
            m_modelSphere = Content.Load<Model>("Models/Sphere");

            m_texture = Content.Load<Texture2D>("Textures/Texture");
            m_normalMap = Content.Load<Texture2D>("Textures/NormalMap");
            m_glossMap = Content.Load<Texture2D>("Textures/GlossMap");
            m_nightMap = Content.Load<Texture2D>("Textures/NightMap");
            
            RemapModel(m_modelSphere, m_effect);
            m_effect.CurrentTechnique = m_effect.Techniques["Technique1"];
        }

        private static void RemapModel(Model a_model, Effect a_effect)
        {
            foreach (ModelMesh mesh in a_model.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    part.Effect = a_effect;
                }
            }
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape) == true)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            DrawSphere(gameTime);

            base.Draw(gameTime);
        }

        private void DrawSphere(GameTime a_gameTime)
        {
            World = Matrix.CreateTranslation(0.0f, 0.0f, -5.0f) * Matrix.CreateScale(1.0f) * Matrix.CreateRotationY((float)a_gameTime.TotalGameTime.Ticks * 1E-7f);
            //World = Matrix.CreateRotationY((float)a_gameTime.TotalGameTime.Ticks * 1E-7f) * Matrix.CreateTranslation(0.0f, 0.0f, -5.0f) * Matrix.CreateScale(1.0f);
            View = Matrix.CreateLookAt(new Vector3(0, 0, -10), Vector3.Zero, Vector3.Up);
            Projection = Matrix.CreatePerspectiveFieldOfView((float)(Math.PI * m_fov / 180.0), GraphicsDevice.Viewport.AspectRatio, 1.0f, 100.0f);

            m_effect.Parameters["World"].SetValue(World);
            m_effect.Parameters["View"].SetValue(View);
            m_effect.Parameters["Projection"].SetValue(Projection);
            m_effect.Parameters["ModelTexture"].SetValue(m_texture);
            m_effect.Parameters["NormalMap"].SetValue(m_normalMap);
            m_effect.Parameters["GlossMap"].SetValue(m_glossMap);
            m_effect.Parameters["NightMap"].SetValue(m_nightMap);
            m_effect.Parameters["LightPos"].SetValue(new Vector3(0, 0, 0));

            DrawMesh(m_modelSphere);
        }

        private void DrawMesh(Model a_model)
        {
            foreach (ModelMesh mesh in a_model.Meshes)
            {
                foreach (Effect e in mesh.Effects)
                {
                    mesh.Draw();
                }
            }
        }
    }
}
